package com.lzl.edu.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <h3>lzl_ws</h3>
 * <p>自定义拦截器</p>
 *
 * @author : 黎钟龙
 * QQ:1661548820
 * Mail：1661548820@qq.com
 * @date : 2022-03-30 20:53
 **/
@Slf4j
public class MyInterceptor implements HandlerInterceptor {

//    前置拦截器（请求发起时开始拦截)
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        log.debug("preHandle开始拦截");
        System.out.println(request.getRequestURI());
        System.out.println("---------");
        System.out.println(request.getContextPath());
        if (request.getRequestURI().equals("/dev/hello")){
            log.debug("preHandle放行");
            log.debug("preHandle拦截结束");
            return true;
        } else {
            log.debug("preHandle拦截");
        }
        log.debug("preHandle拦截结束");
        return false;
    }
//    后置拦截（在preHandle放行后调用）调用时间：Controller方法处理完之后，DispatcherServlet视图渲染之前。
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.debug("postHandle开始执行");
        System.out.println(request.getRequestURI());
        System.out.println("---------");
        System.out.println(request.getContextPath());
        log.debug("postHandle执行结束");
    }
//    请求后执行preHandle放行后调用，
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
